package com.czy.controller;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @Author chenzy
 * @Date 2020/10/16 11:22
 * @Version 1.0
 * @Description
 */
@RestController
public class TestController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;



    @GetMapping("/rocketSendStr")
    public void rocketSendStr() {
        // 消息体为自定义对象
        rocketMQTemplate.convertAndSend("rocket-topic-1", "消息体为字符串");
        System.out.println("消息体为字符串");
    }


    // 延时消息，RocketMQ支持这几个级别的延时消息，不能自定义时长
    // 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
    @GetMapping("/rocketDelayMsgSend")
    public String rocketDelayMsgSend() {
        LocalDateTime currentTime = LocalDateTime.now();
        rocketMQTemplate.syncSend("rocket-topic-1", MessageBuilder.withPayload(new User(1, "张三")).build(), 2000, 3);
        return currentTime.toString();
    }

}
